/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef WISHMASTER_IMAGESTORAGE_H
#define WISHMASTER_IMAGESTORAGE_H

#include "sqlite3/sqlite3.h"
#include "sqlite3/CppSQLite3.h"
#include <QImage>

namespace Wishmaster
{
	struct ImageDescriptor
	{
		QString path;
		int id;
	};

	struct TagInfo
	{
		QString tag;
		int images;
	};

	class ImageStorage
	{
	public:
		ImageStorage();
		virtual ~ImageStorage();

		bool makeDB();
		int addImage(QImage pixmap, QString const& name);
		int addImage(QByteArray arr, QString const& name);
		int addImage(QImage pixmap, const QString& path, const QString& name);
		int addImage(QByteArray arr, const QString& path, const QString& name);
		bool deleteImage(int id);
		void clearTagsForID(int id);
		void addTagForID(int id, QString const& tag);
		int getIDByHash(const QByteArray& hash);
		int getIDByPath(const QString& path);
		QString getPathForID(int id);
		QStringList getTagsForID(int id);
		QList<int> getIDsForTags(QStringList const& tags);
		QStringList getPathsForTags(QStringList const& tags);
		QList<ImageDescriptor> getImagesForTags(const QStringList& tags);
		QImage getImage(int id);
		QString getImageType(int id);
		QList<TagInfo> allTags();
		int countImagesForTag(const QString& tag);

	private:
		QString makePicPath(const QString& initialPath, const QByteArray& hash);
		bool shouldBeSaved(const QByteArray& hash);

		CppSQLite3DB m_sqlite;
		sqlite3* m_db;
		QString m_root;
		QString m_picroot;
	};
}

#endif
